<?php


namespace app\common\exception;

use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;
use Throwable;

class Http extends Handle
{
    public function render($request, Throwable $e): Response
    {
        // 参数验证错误
        if ($e instanceof ValidateException) {

            return json($e->getError(), 422);
        }

        // 请求异常
        if ($e instanceof HttpException) {

            return json(['code'=>$e->getStatusCode(),'msg'=>$e->getMessage()]);
        }
       // return json(['code'=>$e->getCode(),'msg'=>$e->getMessage()]);//系统抛出的异常接不到
        // 其他错误交给系统处理

        return parent::render($request, $e);

    }

}